<extend name="./public/frame.html"/>
<block name="content">
    <form class="layui-form layui-form-pane1" action="" method="post">
        <table class="layui-table">
            <tr>
                <td style="text-align: center;width: 10%">活动名称</td>
                <td><input type="text" name="recharge_name" value="<?php echo $result['recharge_name'];?>" id="recharge_name" lay-verify="required" placeholder="活动名称" class="layui-input" style="width: 500px;"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">请填写活动描述</td>
                <td><textarea placeholder="请输入内容" class="layui-textarea" name="comment" style="width: 500px;"><?php echo $result['comment'];?></textarea></td>
            </tr>
            <tr>
                <td style="text-align: center;">活动时间</td>
                <td>
                    <input type="text" name="start_time" value="<?php echo date('Y-m-d H:i:s',$result['start_time']);?>" id="start_time" lay-verify="required" placeholder="活动开始时间" class="layui-input" style="width: 150px;float: left;margin-right: 10px;">
                    <span style="float: left;margin-right: 10px;padding-top: 5px;">-</span>
                    <input type="text" name="end_time" value="<?php echo date('Y-m-d H:i:s',$result['end_time']);?>" id="end_time" lay-verify="required" placeholder="活动结束时间" class="layui-input" style="width: 150px;float: left;">
                </td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">充值金额（元）</td>
                <td><input type="text" name="money" value="<?php echo $result['money']/100;?>" id="money" lay-verify="required" placeholder="充值金额" class="layui-input" style="width: 500px;" onkeyup="num(this)"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">赠送金额（元）</td>
                <td><input type="text" name="money_return" value="<?php echo $result['money_return']/100;?>" id="money_return" lay-verify="required" placeholder="赠送金额" class="layui-input" style="width: 500px;" onkeyup="num(this)"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">不可提现时间（天）</td>
                <td><input type="text" name="cashable_time" value="<?php echo $result['cashable_time'];?>" id="cashable_time" lay-verify="required" placeholder="不可提现时间（天）" class="layui-input" style="width: 500px;"></td>
            </tr>
            <tr>
                <td style="text-align: center;width: 10%">选择城市</td>
                <td>
                    <div class="layui-input-inline" style="width: 250px;">
                        <select id="type" name="type" lay-filter="type" class="layui-input" style="width: 500px;">
                            <option value="0" <?php if($result['type'] == 0):?>selected<?php endif;?>>全国</option>
                            <option value="1" <?php if($result['type'] == 1):?>selected<?php endif;?>>省份</option>
                            <option value="2" <?php if($result['type'] == 2):?>selected<?php endif;?>>城市</option>
                        </select>
                    </div>
                </td>
            </tr>
            <tr class="ts_province" <?php if($result['type'] == 0):?>style="display: none;"<?php endif;?>>
                <td style="text-align: center;">推送省份</td>
                <td>
                    <div class="layui-form-item" style="margin-bottom: 0px;">
                        <input type="checkbox" data-type="allProvince" lay-filter="allProvince" title="全部" lay-verify="required|province">
                        <div id="provinceArr">
                             <?php foreach($provinceList as $key=>$value):?>
                                <?php if(in_array($value['region_id'],$result['province_id'])):?>
                                <input type="checkbox" name="province_id[]" lay-filter="province" title="{$value.region_name}" value="{$value.region_id}" class="item_child" checked style="margin-top: 5000px;!important;">
                                <?php else:?>
                                <input type="checkbox" name="province_id[]" lay-filter="province" title="{$value.region_name}" value="{$value.region_id}" class="item_child" style="margin-top: 5000px;!important;">
                                <?php endif;?>
                             <?php endforeach;?>
                        </div>
                    </div>
                </td>
            </tr>
            <tr class="ts_city" <?php if($result['type'] != 2):?>style="display: none;"<?php endif;?>>
                <td style="text-align: center;">推送城市</td>
                <td class="tuisong_city">
                    <?php if(!empty($cityList)):?>
                    <?php foreach($result['province_id'] as $key=>$value):?>
                    <div class="layui-form-item" id="cityInfo_<?php echo $value;?>" style="margin-bottom: 0px;">
                        <input type="checkbox" value="<?php echo $value;?>" lay-filter='allCity' title="全部">
                        <?php foreach($cityList as $kml=>$vml):?>
                        <?php if($value == $vml['parent_id']):?>

                        <?php if(in_array($vml['region_id'],$result['region_id'])):?>
                        <input type="checkbox" name="region[]" title="<?php echo $vml['region_name'];?>" value="<?php echo $vml['region_id'];?>" class="item_child_<?php echo $value;?>" checked style="margin-top: 5000px;!important;">
                        <?php else:?>
                        <input type="checkbox" name="region[]" title="<?php echo $vml['region_name'];?>" value="<?php echo $vml['region_id'];?>" class="item_child_<?php echo $value;?>" style="margin-top: 5000px;!important;">
                        <?php endif;?>

                        <?php endif;?>
                        <?php endforeach;?>
                    </div>
                    <?php endforeach;?>
                    <?php endif;?>
                </td>
            </tr>
            <tr>
                <td colspan="3" style="padding-left: 100px;">
                    <input type="hidden" value="<?php echo $result['recharge_id'];?>" id="recharge_id" name="recharge_id">
                    <button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
        </table>
    </form>
    <script>
        layui.use(['form', 'upload', 'laydate'], function () {
            form = layui.form, upload = layui.upload;
            var laydate = layui.laydate;
            laydate.render({
                elem: '#start_time' //指定元素
                ,type: 'datetime'
                ,theme: '#393D49'
            });
            laydate.render({
                elem: '#end_time' //指定元素
                ,type: 'datetime'
                ,theme: '#393D49'
            });
            //选择城市
            form.on('select(type)', function (data) {
                if (data.value == 1) {
                    $(".ts_province").css('display', '')
                    $(".ts_city").css('display', 'none')
                }else if(data.value == 2){
                    $(".ts_province").css('display', '')
                    $(".ts_city").css('display', '')
                    cityInfo();
                }else{
                    $(".ts_province").css('display', 'none')
                    $(".ts_city").css('display', 'none')
                }
            });
            //选择全部省份
            form.on('checkbox(allProvince)', function(data){
                var child = $('#provinceArr').find('.item_child');
                child.each(function (index, item) {
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
                var type = $("#type").val();
                if(type == 2){
                    cityInfo()
                }
            });
            //选择单个省份
            form.on('checkbox(province)', function(data){
                cityInfo()
            });
            //选择省份 + 载入城市
            function cityInfo(){
                var type = $("#type").val();
                if(type == 2){
                    var items = '';
                    $('.item_child:checked').each(function(){
                        items += this.value + ',';
                    });
                    items = items.substr(0, (items.length - 1));
                    $.post('/admin/Promotion/cityInfoMation', {'region_id': items}, function (res) {
                        $(".tuisong_city").html(res);
                        form.render();
                    })
                }
            }
            //选择全部 + 城市全部勾选
            form.on('checkbox(allCity)', function(data){
                var id = data.value;
                var child = $('#cityInfo_'+id).find('.item_child_'+id);
                child.each(function (index, item) {
                    item.checked = data.elem.checked;
                });
                form.render('checkbox');
            })
            //活动数据提交
            form.on('submit(*)', function (data) {
                var start_time = $("#start_time").val();
                var end_time = $("#end_time").val();
                if (start_time >= end_time) {
                    layer.msg('活动开始时间不能大于结束时间', {icon: 5});
                    return false
                }
                $.ajax({
                    url:'{:url("/admin/Promotion/edit")}',
                    async:true,
                    dataType:'json',
                    type:'POST',
                    data: $(data.form).serialize(),
                    success:function(res){
                        if(res.code==1){
                            layer.alert(res.msg, {icon: 6},function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                window.parent.location.reload()
                            });
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                return false;

            })
        })
        //验证只能输入数字
        function num(object) {
            var value   = object.value;
            var id      = object.id;
            if (isNaN(value)){
                layer.msg('请输入正确的数字',{icon: 5});
                $("#"+id).val('');
                return false;
            }
        }
    </script>
</block>
